import { MessageEnum, MessageBoxEnum,MessageTypeEnum } from "@/config";
import {
  ElMessageBox,
  type ElMessageBoxOptions,
  ElMessage,
  type MessageParamsWithType,
} from "element-plus";
import type { AppContext, VNode } from "vue";

export type messageType = MessageEnum

export type messageBoxType = MessageBoxEnum

export interface MessageBoxBaseParams {
  message?: string | VNode | (() => VNode);
  title?: string | ElMessageBoxOptions;
  options?: ElMessageBoxOptions;
  appContext?: AppContext | null;
}

export interface MessageBoxParams extends MessageBoxBaseParams {
  type: messageBoxType
}

export interface MessageBaseParams {
  options?: MessageParamsWithType;
  appContext?: AppContext | null;
}

export interface MessageParams extends MessageBaseParams {
  type: messageType
}


function createInfoMessage(opt: MessageBaseParams) {
  ElMessage.info(opt.options, opt.appContext);
}

function createSuccessMessage(opt: MessageBaseParams) {
  ElMessage.success(opt.options, opt.appContext);
}

function createWarningMessage(opt: MessageBaseParams) {
  ElMessage.warning(opt.options, opt.appContext);
}

function createErrorMessage(opt: MessageBaseParams) {
  ElMessage.error(opt.options, opt.appContext);
}

function createMessageHandle(type:MessageEnum) {

  let result : (opt: MessageBaseParams) => void;
  switch (type) {
    case MessageEnum.INFO:
      result = createInfoMessage
      break;
    case MessageEnum.SUCCESS:
      result = createSuccessMessage
      break;
    case MessageEnum.WARINING:
      result = createWarningMessage
      break;
    case MessageEnum.ERROR:
      result = createErrorMessage
      break;
  }

  return result

}

function createConfirmMessageBox(opt: MessageBoxBaseParams) {
  ElMessageBox.confirm(opt.message, opt.title, opt.options, opt.appContext);
}

function createMessageBox(opt: MessageBoxBaseParams) {
  ElMessageBox.alert(opt.message, opt.title, opt.options, opt.appContext);
}

function createMessageBoxHandle(type:MessageBoxEnum) {

  let result : (opt: MessageBoxBaseParams) => void;
  
  switch (type) {
    case MessageBoxEnum.ALERT:
      result = createMessageBox
      break;
    case MessageBoxEnum.CONFIRM:
      result = createConfirmMessageBox
      break;
  }

  return result
}


function createMessage(type: MessageTypeEnum.MESSAGE,modeType:MessageEnum): (opt: MessageBaseParams) => void;
function createMessage(type: MessageTypeEnum.MESSAGEBOX,modeType:MessageBoxEnum): (opt: MessageBoxBaseParams) => void;
function createMessage(type: MessageTypeEnum,modeType:MessageEnum | MessageBoxEnum) {

  let result;
  switch(type){
    case MessageTypeEnum.MESSAGE:
      result = createMessageHandle(modeType as MessageEnum)
      break
    case MessageTypeEnum.MESSAGEBOX:
      result = createMessageBoxHandle(modeType as MessageBoxEnum)
      break  
  }
  return result

}

export default function useMessage() {
  return {
    createMessageBox,
    createConfirmMessageBox,
    createInfoMessage,
    createSuccessMessage,
    createWarningMessage,
    createErrorMessage,
    createMessageHandle,
    createMessageBoxHandle,
    createMessage
  };
}
